import { Component, OnInit,Output } from '@angular/core';
import { ActivatedRoute, ActivatedRouteSnapshot, Router } from '@angular/router';
import * as moment from 'moment';

import { PageTitle } from './page-title';
import { Day } from './day';
import { User } from './user';
import { UserService } from './user.service';
import { Task } from './task';
import { Project } from './project';
import { TaskService } from './task.service';
import { ProjectService } from './project.service';

@Component({
  selector: 'tasks',
  templateUrl: "./tasks.component.html",
  styleUrls: [ "./tasks.component.css" ]
})
export class TasksComponent implements OnInit {
  user_id: string
  user: User = new User()
  showDate: Day
  mine: User
  tasks: Array<Task>
  projects: Array<Project>
  top_date: string
  bottom_date: string
  select_date: string
  date: string
  show_day: string
  show_week: string

  constructor(private route: ActivatedRoute,
              private userService: UserService,
              private taskService: TaskService,
              private projectService: ProjectService,
              private pageTitle: PageTitle,
              private router: Router) { }

  ngOnInit(): void {
    this.pageTitle.back = null
    this.pageTitle.title = "任务表"

    this.userService.getUserMine().then(user => {
      if (!user) return false
      this.mine = user
    })
    moment.locale('en', {
      weekdays : [
          "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
      ]
    });
    this.date = moment().format('YYYY年MM月')
    this.show_day = moment().format('DD')
    this.show_week = moment().format("dddd")
    this.route.params.subscribe(params => {
      this.user_id = params['user_id']
      this.userService.get(this.user_id).then(user => {
        this.user = user
        if (this.user_id != "mine")
          this.pageTitle.back = "back"
        this.pageTitle.title = `${this.user.name}的任务表`
      })
    })
  }

  onCalendarSelected(sel_days: Array<Day>): void {
    this.changeShowDate(sel_days[0])
  }

  changeShowDate(day: Day) {
    this.showDate = day
    this.select_date = `${day.y}-${day.m+1}-${day.num}`
    var tp = moment(this.select_date,"YYYY-MM-DD")
    var standard = tp.subtract(15, 'days')
    this.top_date = standard.format("MM-DD");
    this.bottom_date = tp.add(30, 'days').format("MM-DD");
    var argv = { receiver_id: this.user_id,
                 date: this.select_date,
                 contain: "project,milestones,dailys,receiver" }
    this.taskService.getList(argv).then(tasks => {
      this.tasks = tasks.filter(task => {
        task.s_date = moment( task.s_date).format("MM-DD")
        task.e_date = moment( task.e_date).format("MM-DD")
        task.s_width = this.compute(task.s_date, this.top_date) 
        task.e_width = this.compute(task.e_date, this.top_date)
        var w = task.e_width - task.s_width 
        if (w > 100) {
            task.s_e_width =  100
        }else {
           task.s_e_width = task.e_width - task.s_width  
        }
        if (task.daily){
          var content = task.daily.report+ task.daily.next_work
          if(content == '0'){
            task.daily = null
          }
          else {
             task.daily.content = content
          }
        }

        task.milestones.filter(milestone => {
            milestone.date = moment( milestone.date).format("MM-DD")
            milestone.l_width = this.compute(milestone.date, this.top_date)
            return milestone
        })
        return task
      })
    })
  }

  goProject(project): boolean {
    this.router.navigate(["projects", project.id])
    return false
  }

  changeTask(task, field, value): void {
     switch(value){
       case "processing" :
        task[field] = value
         if(!this.user.manager){
           this.taskService.update(task,field)
         }
         else{
           task[field] = 'pending'
           alert('权限不符合')
         }
        
      break; 
      case "solved" :
        task[field] = value
       if(this.user.manager){
        this.taskService.update(task,field)
      }
      else {
         task[field] = 'processing'
         alert('权限不足')
      }
      break; 
      case "cancel" :
        task[field] = value
      if(this.user.manager){
        this.taskService.update(task,field)
      }
      else{
         task[field] = 'solved'
        alert('权限不足')
      }
      break;  
    }
  }

  public compute(s_date, t_date): number {
    var w 
    var s = moment(s_date)
    var e = moment(t_date)
    var count = s.diff(e, 'days')
    if(count > 0){
      w = (count /30)*100 
    }
    else {
      w = 0 
    }
    return  Math.round(w)
  }
}
